package com.int_tech.shebeiManager.controller;

import com.int_tech.data.dao.DataInfoDao;
import com.int_tech.data.entity.DataInfo;
import com.int_tech.shebeiManager.dao.ShebeiDao;
import com.int_tech.shebeiManager.entity.Shebei;
import com.int_tech.utils.DateTool;
import com.int_tech.utils.KuaYu;
import com.int_tech.utils.Result;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhoupeiwu
 * @Title: NewSheBeiController
 * @ProjectName HYJ
 * @Description: TODO
 * @date 2018/12/31下午10:36
 */
@CrossOrigin
@RestController
@RequestMapping("/newSheBei")
public class NewSheBeiController {

    @Autowired
    private ShebeiDao shebeiDao;

    @Autowired
    private DataInfoDao dataInfoDao;

    /**
     * 获取设备列表
     *
     * @param request
     * @param response
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/getSheBeiList")
    public Result getShebeiList(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {

        Sort sort = new Sort(Sort.Direction.ASC,"shebeiName");

        List<Shebei> shebeiList = shebeiDao.findAll(sort);

        List<Shebei> newSheBeiList = new ArrayList<Shebei>();

        String html = "";

        for (Shebei shebei : shebeiList) {

            String shebeiName = shebei.getShebeiName();

            if(org.apache.commons.lang.StringUtils.isEmpty(shebeiName))
            {
                shebeiName = shebei.getShebeiId().substring(shebei.getShebeiId().length()-8,shebei.getShebeiId().length());
            }
            //查看停机状态A3
            List<DataInfo> dataInfos = dataInfoDao.findOneByShebeiIdAndZhenTouOrderByCreateTimeDesc(shebei.getShebeiId(),"A3");

            DataInfo dataInfo = new DataInfo();

            if(dataInfos == null || dataInfos.size() == 0)
            {
                dataInfo = null;
            }
            else
            {
                dataInfo = dataInfos.get(0);
            }

            if(dataInfo == null)
            {
                shebei.setOnline(-1);
            }
            else
            {
                String A3Time = DateTool.Date2String(dataInfo.getCreateTime(),"yyyy-MM-dd");

                String now = DateTool.Date2String(new Date(),"yyyy-MM-dd");

                if(now.equals(A3Time))
                {
                    shebei.setOnline(1);
                }
                else
                {
                    shebei.setOnline(-1);
                }
            }
            newSheBeiList.add(shebei);
        }

        return new Result(newSheBeiList);

    }

    /**
     * 定时获取设备列表
     * @param shebeiIdList
     * @param request
     * @param response
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/getShebeiListOnTime")
    public Result getShebeiListOnTime(String shebeiIdList,HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {

        Sort sort = new Sort(Sort.Direction.ASC,"shebeiName");

        List<Shebei> shebeiList = shebeiDao.findAll(sort);

        List<Shebei> newSheBeiList = new ArrayList<Shebei>();

        String html = "";

        for(Shebei shebei : shebeiList)
        {
            if("A3001002003006007223344558".equals(shebei.getShebeiId()))
            {
                System.out.print(111);
            }
            String shebeiName = shebei.getShebeiName();

            if(shebeiIdList.contains(shebei.getShebeiId()))
            {
                if(org.apache.commons.lang.StringUtils.isEmpty(shebeiName))
                {
                    shebeiName = shebei.getShebeiId().substring(shebei.getShebeiId().length()-8,shebei.getShebeiId().length());
                }
                //查看停机状态A3
                List<DataInfo> dataInfos = dataInfoDao.findOneByShebeiIdAndZhenTouOrderByCreateTimeDesc(shebei.getShebeiId(),"A4");

                List<DataInfo> dataInfos2 = dataInfoDao.findOneByShebeiIdAndZhenTouOrderByCreateTimeDesc(shebei.getShebeiId(),"A3");

                if(dataInfos == null || dataInfos.size() == 0)
                {
                    dataInfos = dataInfos2;
                }
                else
                {
                    dataInfos.addAll(dataInfos2);
                }

                DataInfo dataInfo = new DataInfo();

                if(dataInfos == null || dataInfos.size() == 0)
                {
                    dataInfo = null;
                }
                else
                {
                    dataInfo = dataInfos.get(0);
                }

                if(dataInfo == null)
                {
                    shebei.setOnline(-1);
                }
                else
                {
                    String A3Time = DateTool.Date2String(dataInfo.getCreateTime(),"yyyy-MM-dd");

                    String now = DateTool.Date2String(new Date(),"yyyy-MM-dd");

                    if(now.equals(A3Time))
                    {
                        shebei.setOnline(1);
                    }
                    else
                    {
                        shebei.setOnline(-1);
                    }
                }
                newSheBeiList.add(shebei);
            }


        }

        return new Result(newSheBeiList);
    }

    /**
     * 修改设备信息
     * @param request
     * @param response
     * @param shebeiId
     * @param shebeiName
     * @param wendufz
     * @param shidufz
     * @param xfz
     * @param yfz
     * @param zfz
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/updateShebei")
    public String updateShebei(HttpServletRequest request, HttpServletResponse response,String shebeiId,String shebeiName,String wendufz,String shidufz,String xfz,String yfz,String zfz) throws UnsupportedEncodingException {

        KuaYu.kuaYuResponse(response);

        Shebei shebei = shebeiDao.findByShebeiId(shebeiId);

        if(shebei == null)
        {
            shebei = new Shebei();
            shebei.setStatus(0);
            shebei.setCreateTime(new Date());
            shebei.setShebeiId(shebeiId);
        }
//        shebei.setStatus(0);
        shebei.setShebeiName(shebeiName);
        shebei.setUpdateTime(new Date());
        shebei.setWendufz(wendufz);
        shebei.setShidufz(shidufz);
        shebei.setXfz(xfz);
        shebei.setYfz(yfz);
        shebei.setZfz(zfz);

        shebeiDao.save(shebei);
        JSONObject jsonObject = new JSONObject();

        jsonObject.put("status","success");
        return jsonObject.toString();
    }

}
